package com.atguigu.guli.service.edu.controller.admin;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.TeacherQuery;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.atguigu.guli.service.edu.service.client.ServiceOssFeignClient;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-10-26
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/edu/teacher")
@Slf4j
public class AdminTeacherController {

    @Autowired
    TeacherService teacherService;
    @Autowired
    ServiceOssFeignClient   serviceOssFeignClient;

    @PostMapping("/add")
    public R add(@RequestBody Teacher teacher){
        teacherService.save(teacher);
        return R.setResult(ResultCodeEnum.SUCCESS);
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id){
        try {
            Teacher teacher = teacherService.getById(id);
            boolean result = teacherService.removeById(id);
            String avatar = teacher.getAvatar();
            if(!StringUtils.isEmpty(avatar)){
                serviceOssFeignClient.delete(avatar,"avatar");
            }
            if(result){
                return R.ok().message("删除成功");
            }else{
                return R.error().message("数据不存在");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @PutMapping("/update")
    public R update(@RequestBody Teacher teacher){
        try {
            teacherService.updateById(teacher);
            return R.setResult(ResultCodeEnum.SUCCESS);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/list")
    public R queryAll(){
        List<Teacher> list = teacherService.list();
        if (list!=null){
            return R.ok().data("list",list);
        }
        return R.error().message("教师信息为空");
    }

    @GetMapping("/queryById/{id}")
    public R queryById(@PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        if(teacher!=null){
            return R.ok().data("teacher",teacher);
        }
        return R.error().message("查询教师信息不存在");
    }

    @GetMapping("/list/{pageNum}/{pageSize}")
    public R listPageByQuery(@PathVariable Integer pageNum, @PathVariable Integer pageSize, TeacherQuery teacherQuery){
//        Page<Teacher> page = teacherService.page(new Page<>(pageNum, pageSize));
        IPage<Teacher> page = new Page<>(pageNum,pageSize);
        teacherService.pageByCondition(teacherQuery,page);
        return R.ok().data("page",page);
    }
}

